/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package email.api;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.postgresql.core.Utils;
import org.postgresql.util.Base64;

/**
 *
 * @author Puma
 */
public class ReadUrl {

    private Postgresql psql = new Postgresql();
    private StringUtils str = new StringUtils();

    public void ReadUrl(String address, int id) {

        String _img = null;
        String _url = null;
        String _text = null;
        String _aux = null;
        BufferedReader in;
        try {

            if (id == 1) {
                URL url = new URL(address);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();

                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            } else {
                File archivo = new File(address);
                FileReader fr = new FileReader(archivo);
                in = new BufferedReader(fr);
            }
            String html = "";
            String linea;

            Delete_Deals_Site(id); //Borra los cupones viejos

            while ((linea = in.readLine()) != null) {
                html += linea;

                String patron_img = "<img\\s+[^>]*src=[\"']([^>\"']*)[\"'][^>]*>";
                String patron_href = "<a\\s+[^>]*href=[\"']([^>\"']*)[\"'][^>]*>";
                String patron_text = "<a\\s+[^>]*href=[\"']([^\\\\\\\"]+).+?>(.+?)</a>";

                Pattern p_img = Pattern.compile(patron_img);
                Pattern p_href = Pattern.compile(patron_href);
                Pattern p_text = Pattern.compile(patron_text);

                Matcher matcher_img = p_img.matcher(linea);
                Matcher matcher_href = p_href.matcher(linea);
                Matcher matcher_text = p_text.matcher(linea);

                String patron_texto = ".*haga click.*"; //Valida si contiene el texto "haga click"
                Pattern p_texto = Pattern.compile(patron_texto);

                String patron_texto1 = ".*ver el plan.*"; //Valida si contiene el texto "ver plan"
                Pattern p_texto1 = Pattern.compile(patron_texto1);

                _aux = null;
                if (matcher_img.find()) {
                    _img = matcher_img.group(1);
                    _aux = _img;
                }
                if (matcher_href.find()) {
                    _url = matcher_href.group(1);
                }
                if (matcher_text.find()) {
                    Matcher matcher_aux = p_img.matcher(matcher_text.group(2));
                    if (matcher_aux.find()) {
                        _img = matcher_aux.group(1);
                    } else {
                        _text = matcher_text.group(2);
                    }
                }

                if ((_img != null) && (_url != null) && (_text != null)) {
                    Deal deal = new Deal(_img, _url, _text, 0, 0, "cupon", 0, 0, 0, 0);

                    Matcher matcher_texto = p_texto.matcher(deal.getName());
                    Matcher matcher_texto1 = p_texto1.matcher(deal.getName());
                    if ((!matcher_texto.find()) && (!matcher_texto1.find())) {
                        deal = Search_Category(deal);
                        deal = Search_Site(deal);

                        deal = Search_Price(deal);

                        if (deal.getAmount() != 0) {
                            int discount = 0;
                            if (deal.getAmount() >= deal.getPrice()) {
                                discount = (int) (abs(deal.getPrice() / (double) deal.getAmount()) * 100);
                            } else {
                                discount = (int) (abs(deal.getAmount() / (double) deal.getPrice()) * 100);
                            }
                            deal.setDiscount(discount);
                        }

                        String sql = "INSERT INTO DEALS(NAME,AMOUNT,PRICE,DISCOUNT,IMAGE,URL,TIPO,VISIT,SITE_ID,CATEGORIE_ID) VALUES "
                                + "(\'" + str.unescapeHTML(deal.getName()) + "\',\'" + deal.getAmount() + "\',\'" + deal.getPrice() + "\',\'" + deal.getDiscount() + "\',\'" + deal.getImg()
                                + "\',\'" + deal.getUrl() + "\',\'" + deal.getTipo() + "\'," + deal.getVisit() + "," + deal.getSite() + "," + deal.getCategory() + ");";
                        psql.insert(sql);
                        System.out.println(sql);
                        //System.out.println("texto-> " + deal.getName());
                        //System.out.println("img-> " + deal.getImg());
                        //System.out.println("url-> " + deal.getUrl());
                        if ((_img != null) && (_url != null) && (_text != null)) {
                            _img = _url = _text = null;
                        }
                    }
                    //System.out.println("------");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void ReadFile(String address, int id) {

        String _img = null;
        String _url = null;
        String _text = null;
        String _aux = null;
        BufferedReader in;
        try {
            File archivo = new File(address);
            FileReader fr = new FileReader(archivo);
            in = new BufferedReader(fr);

            String html = "";
            String linea;

            Delete_Deals_Site(id); //Borra los cupones viejos

            while ((linea = in.readLine()) != null) {
                html += linea;
                String patron_img = "<img\\s+[^>]*src=[\"']([^>\"']*)[\"'][^>]*>";
                String patron_href = "<a\\s+[^>]*href=[\"']([^>\"']*)[\"'][^>]*>";
                //String patron_text = "<a\\s+[^>]*href=[\"']([^\\\\\\\"]+).+?>(.+?)</a>";
                String patron_text = "<b>(.+?)</b>";

                Pattern p_img = Pattern.compile(patron_img);
                Pattern p_href = Pattern.compile(patron_href);
                Pattern p_text = Pattern.compile(patron_text);

                Matcher matcher_img = p_img.matcher(linea);
                Matcher matcher_href = p_href.matcher(linea);
                Matcher matcher_text = p_text.matcher(linea);

                _aux = null;
                if (matcher_img.find()) {
                    _img = matcher_img.group(1);
                    _aux = _img;
                }
                if (matcher_href.find()) {
                    _url = matcher_href.group(1);
                }
                if (matcher_text.find()) {
                    _text = matcher_text.group(1);
                }

                if ((_img != null) && (_url != null) && (_text != null)) {
                    Deal deal = new Deal(_img, _url, _text, 0, 0, "cupon", 0, 0, 0, 0);

                    deal = Search_Category(deal);
                    deal.setSite(id);

                    deal = Search_Price(deal);

                    if (deal.getAmount() != 0) {
                        int discount = 0;
                        if (deal.getAmount() >= deal.getPrice()) {
                            discount = (int) (abs(deal.getPrice() / (double) deal.getAmount()) * 100);
                        } else {
                            discount = (int) (abs(deal.getAmount() / (double) deal.getPrice()) * 100);
                        }
                        deal.setDiscount(discount);
                    }

                    String sql = "INSERT INTO DEALS(NAME,AMOUNT,PRICE,DISCOUNT,IMAGE,URL,TIPO,VISIT,SITE_ID,CATEGORIE_ID) VALUES "
                            + "(\'" + str.unescapeHTML(deal.getName()) + "\',\'" + deal.getAmount() + "\',\'" + deal.getPrice() + "\',\'" + deal.getDiscount() + "\',\'" + deal.getImg()
                            + "\',\'" + deal.getUrl() + "\',\'" + deal.getTipo() + "\'," + deal.getVisit() + "," + deal.getSite() + "," + deal.getCategory() + ");";
                    psql.insert(sql);

                    System.out.println(sql);
                    
                    //System.out.println("texto-> " + deal.getName());
                    //System.out.println("img-> " + deal.getImg());
                    //System.out.println("url-> " + deal.getUrl());
                    if ((_img != null) && (_url != null) && (_text != null)) {
                        _img = _url = _text = null;
                    }
                    //System.out.println("------");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void ReadFile2(String address, int id) {

        String _img = null;
        String _url = null;
        String _text = null;
        String _aux = null;
        BufferedReader in;
        try {
            if (id == 1) {
                URL url = new URL(address);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();

                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            } else {
                File archivo = new File(address);
                FileReader fr = new FileReader(archivo);
                in = new BufferedReader(fr);
            }
            String html = "";
            String linea;

            Delete_Deals_Site(id); //Borra los cupones viejos

            while ((linea = in.readLine()) != null) {
                html += linea;

                String patron_img = "<img\\s+[^>]*src=[\"']([^>\"']*)[\"'][^>]*>";
                String patron_href = "<a\\s+[^>]*href=[\"']([^>\"']*)[\"'][^>]*>";
                String patron_text = "<a\\s+[^>]*href=[\"']([^\\\\\\\"]+).+?>(.+?)</a>";

                Pattern p_img = Pattern.compile(patron_img);
                Pattern p_href = Pattern.compile(patron_href);
                Pattern p_text = Pattern.compile(patron_text);

                Matcher matcher_img = p_img.matcher(linea);
                Matcher matcher_href = p_href.matcher(linea);
                Matcher matcher_text = p_text.matcher(linea);

                String patron_texto = ".*haga click.*"; //Valida si contiene el texto "haga click"
                Pattern p_texto = Pattern.compile(patron_texto);

                String patron_texto1 = ".*ver el plan.*"; //Valida si contiene el texto "ver plan"
                Pattern p_texto1 = Pattern.compile(patron_texto1);
                
                String patron_texto2 = ".*@tudesc.*"; //Valida si contiene el texto "ver plan"
                Pattern p_texto2 = Pattern.compile(patron_texto2);
                
                String patron_texto3 = ".*fb.me.*"; //Valida si contiene el texto "ver plan"
                Pattern p_texto3 = Pattern.compile(patron_texto3);
                
                String patron_texto4 = ".*link.*"; //Valida si contiene el texto "ver plan"
                Pattern p_texto4 = Pattern.compile(patron_texto4);

                _aux = null;
                if (matcher_img.find()) {
                    _img = matcher_img.group(1);
                    _aux = _img;
                }
                if (matcher_href.find()) {
                    _url = matcher_href.group(1);
                }
                if (matcher_text.find()) {
                    Matcher matcher_aux = p_img.matcher(matcher_text.group(2));
                    if (matcher_aux.find()) {
                        _img = matcher_aux.group(1);
                    } else {
                        _text = matcher_text.group(2);
                    }
                }

                if ((_img != null) && (_url != null) && (_text != null)) {
                    Deal deal = new Deal(_img, _url, _text, 0, 0, "cupon", 0, 0, 0, 0);

                    Matcher matcher_texto = p_texto.matcher(deal.getName());
                    Matcher matcher_texto1 = p_texto1.matcher(deal.getName());
                    Matcher matcher_texto2 = p_texto2.matcher(deal.getName());
                    Matcher matcher_texto3 = p_texto3.matcher(deal.getName());
                    Matcher matcher_texto4 = p_texto4.matcher(deal.getName());
                    
                    if ((!matcher_texto.find()) && (!matcher_texto1.find())  && 
                            (!matcher_texto2.find())  && (!matcher_texto3.find()) && (!matcher_texto4.find())) {
                        
                        deal = Search_Category(deal);
                        deal = Search_Site(deal);

                        deal = Search_Price(deal);

                        if (deal.getAmount() != 0) {
                            int discount = 0;
                            if (deal.getAmount() >= deal.getPrice()) {
                                discount = (int) (abs(deal.getPrice() / (double) deal.getAmount()) * 100);
                            } else {
                                discount = (int) (abs(deal.getAmount() / (double) deal.getPrice()) * 100);
                            }
                            deal.setDiscount(discount);
                        }

                        String sql = "INSERT INTO DEALS(NAME,AMOUNT,PRICE,DISCOUNT,IMAGE,URL,TIPO,VISIT,SITE_ID,CATEGORIE_ID) VALUES "
                                + "(\'" + str.unescapeHTML(deal.getName()) + "\',\'" + deal.getAmount() + "\',\'" + deal.getPrice() + "\',\'" + deal.getDiscount() + "\',\'" + deal.getImg()
                                + "\',\'" + deal.getUrl() + "\',\'" + deal.getTipo() + "\'," + deal.getVisit() + "," + deal.getSite() + "," + deal.getCategory() + ");";
                        psql.insert(sql);
                        System.out.println(sql);
                        //System.out.println("texto-> " + deal.getName());
                        //System.out.println("img-> " + deal.getImg());
                        //System.out.println("url-> " + deal.getUrl());
                        if ((_img != null) && (_url != null) && (_text != null)) {
                            _img = _url = _text = null;
                        }
                    }
                    //System.out.println("------");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private Deal Search_Site(Deal deal) {
        String url = deal.getUrl();
        String patron_alt1 = ".*aprovecha.com.*";
        String patron_alt2 = ".*tudescuenton.com.*";
        String patron_alt3 = ".*tremendodescuento.com.*";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);

        Matcher matcher_texto1 = p_alt1.matcher(url);
        Matcher matcher_texto2 = p_alt2.matcher(url);
        Matcher matcher_texto3 = p_alt3.matcher(url);

        if (matcher_texto1.find()) {
            deal.setSite(1);
        } else if (matcher_texto2.find()) {
            deal.setSite(2);
        } else if (matcher_texto3.find()) {
            deal.setSite(3);
        }

        return deal;
    }

    private void Delete_Deals_Site(int id) {
        String delete = "DELETE FROM DEALS WHERE SITE_ID =" + id;
        psql.delete(delete);
    }

    private Deal Search_Category(Deal deal) {

        if (deal.getCategory() == 0) {
            deal.setCategory(comida(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(deportes_fitness(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(viajes(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(vehiculos(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(eventos(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(salud_belleza(deal.getName()));
        }
        if (deal.getCategory() == 0) {
            deal.setCategory(7);
        }

        return deal;
    }

    private int comida(String name) {
        int id = 0;
        String patron_alt1 = ".*[Hh]elad.*";
        String patron_alt2 = ".*[pP]izza.*";
        String patron_alt3 = ".*[sS]ushi.*";
        String patron_alt4 = ".*[rR]estaurante.*";
        String patron_alt5 = ".*[pP]arrilla.*";
        String patron_alt6 = ".*[cC]arne.*";
        String patron_alt7 = ".*[Pp]ollo.*";
        String patron_alt8 = ".*[Cc]onsum.*";
        String patron_alt9 = ".*[Cc]ocin.*";
        String patron_alt10 = ".*[Pp]ostre.*";
        String patron_alt11 = ".*[Pp]lato.*";
        //String patron_alt12 = ".*[Cc]hocolate.*";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
        Pattern p_alt7 = Pattern.compile(patron_alt7);
        Pattern p_alt8 = Pattern.compile(patron_alt8);
        Pattern p_alt9 = Pattern.compile(patron_alt9);
        Pattern p_alt10 = Pattern.compile(patron_alt10);
        Pattern p_alt11 = Pattern.compile(patron_alt11);
        //Pattern p_alt12 = Pattern.compile(patron_alt12);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
        Matcher matcher_texto7 = p_alt7.matcher(name);
        Matcher matcher_texto8 = p_alt8.matcher(name);
        Matcher matcher_texto9 = p_alt9.matcher(name);
        Matcher matcher_texto10 = p_alt10.matcher(name);
        Matcher matcher_texto11 = p_alt11.matcher(name);
        //Matcher matcher_texto12 = p_alt12.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find())
                || (matcher_texto7.find()) || (matcher_texto8.find()) || (matcher_texto9.find())
                || (matcher_texto10.find()) || (matcher_texto11.find())/* || (matcher_texto12.find())*/) {
            id = 1;
        } else {
            id = 0;
        }
        return id;
    }

    private int deportes_fitness(String name) {
        int id = 0;
        String patron_alt1 = ".*[Gg]imnasi.*";
        String patron_alt2 = ".*[Gg]ym.*";
        String patron_alt3 = ".*[Pp]ilate.*";
        String patron_alt4 = ".*[Tt][Rr][Xx].*";
        String patron_alt5 = ".*[Pp]arapente.*";
        String patron_alt6 = ".*[Bb]uceo.*";
        String patron_alt7 = ".*[Pp]ole.*";
        String patron_alt8 = ".*[Dd]eport.*"; 
//        String patron_alt9 = ".*[Cc]ocin.*"; 

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
        Pattern p_alt7 = Pattern.compile(patron_alt7);
        Pattern p_alt8 = Pattern.compile(patron_alt8);
//        Pattern p_alt9 = Pattern.compile(patron_alt9);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
        Matcher matcher_texto7 = p_alt7.matcher(name);
        Matcher matcher_texto8 = p_alt8.matcher(name);
//        Matcher matcher_texto9 = p_alt9.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find())
                || (matcher_texto7.find()) || (matcher_texto8.find())/* || (matcher_texto9.find())*/) {
            id = 2;
        } else {
            id = 0;
        }
        return id;
    }

    private int viajes(String name) {
        int id = 0;
        String patron_alt1 = ".*[Mm]argarita.*";
        String patron_alt2 = ".*[Ff]ull [Dd]ay.*";
        String patron_alt3 = ".*[Pp]laya.*";
        String patron_alt4 = ".*[Pp]osada.*";
        String patron_alt5 = ".*[Hh]acienda.*";
        String patron_alt6 = ".*[Tt]ortuga.*";
        String patron_alt7 = ".*[Hh]otel.*";
        String patron_alt8 = ".*[Tt]uris.*";
        String patron_alt9 = ".*[Tt]our.*";
        String patron_alt10 = ".*[Hh]iguerote.*";
        String patron_alt11 = ".*[Mm]anantial.*";
        String patron_alt12 = ".*[Pp]lan [Vv]acaion.*";
        String patron_alt13 = ".*[Vv]acacion.*";
        String patron_alt14 = ".*[Hh]ospedaje.*";
        String patron_alt15 = ".*[Ss]uite.*";
        String patron_alt16 = ".*[Ff]ull[Dd]ay.*";
        String patron_alt17 = ".*[Hh]abitaci.*";
        String patron_alt18 = ".*[Cc]ampamento.*";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
        Pattern p_alt7 = Pattern.compile(patron_alt7);
        Pattern p_alt8 = Pattern.compile(patron_alt8);
        Pattern p_alt9 = Pattern.compile(patron_alt9);
        Pattern p_alt10 = Pattern.compile(patron_alt10);
        Pattern p_alt11 = Pattern.compile(patron_alt11);
        Pattern p_alt12 = Pattern.compile(patron_alt12);
        Pattern p_alt13 = Pattern.compile(patron_alt13);
        Pattern p_alt14 = Pattern.compile(patron_alt14);
        Pattern p_alt15 = Pattern.compile(patron_alt15);
        Pattern p_alt16 = Pattern.compile(patron_alt16);
        Pattern p_alt17 = Pattern.compile(patron_alt17);
        Pattern p_alt18 = Pattern.compile(patron_alt18);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
        Matcher matcher_texto7 = p_alt7.matcher(name);
        Matcher matcher_texto8 = p_alt8.matcher(name);
        Matcher matcher_texto9 = p_alt9.matcher(name);
        Matcher matcher_texto10 = p_alt10.matcher(name);
        Matcher matcher_texto11 = p_alt11.matcher(name);
        Matcher matcher_texto12 = p_alt12.matcher(name);
        Matcher matcher_texto13 = p_alt13.matcher(name);
        Matcher matcher_texto14 = p_alt14.matcher(name);
        Matcher matcher_texto15 = p_alt15.matcher(name);
        Matcher matcher_texto16 = p_alt16.matcher(name);
        Matcher matcher_texto17 = p_alt17.matcher(name);
        Matcher matcher_texto18 = p_alt18.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find())
                || (matcher_texto7.find()) || (matcher_texto8.find()) || (matcher_texto9.find())
                || (matcher_texto10.find()) || (matcher_texto11.find()) || (matcher_texto12.find())
                || (matcher_texto13.find()) || (matcher_texto14.find()) || (matcher_texto15.find())
                || (matcher_texto16.find()) || (matcher_texto17.find()) || (matcher_texto18.find())) {
            id = 3;
        } else {
            id = 0;
        }
        return id;
    }

    private int vehiculos(String name) {
        int id = 0;
        String patron_alt1 = ".*[Pp]apel [Aa]humado.*";
        String patron_alt2 = ".*[Ll]atoner.*";
        String patron_alt3 = ".*[Cc]arro.*";
        String patron_alt4 = ".*[Cc]amioneta.*";
        String patron_alt5 = ".*[Aa]utomotriz.*";
        String patron_alt6 = ".*[Vv]ehiculo.*";
//        String patron_alt7 = ".*[Pp]ollo.*"; 
//        String patron_alt8 = ".*[Cc]onsum.*"; 
//        String patron_alt9 = ".*[Cc]ocin.*"; 

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
//        Pattern p_alt7 = Pattern.compile(patron_alt7);
//        Pattern p_alt8 = Pattern.compile(patron_alt8);
//        Pattern p_alt9 = Pattern.compile(patron_alt9);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
//        Matcher matcher_texto7 = p_alt7.matcher(name);
//        Matcher matcher_texto8 = p_alt8.matcher(name);
//        Matcher matcher_texto9 = p_alt9.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find()) /*|| (matcher_texto7.find()) || (matcher_texto8.find()) || (matcher_texto9.find())*/) {
            id = 4;
        } else {
            id = 0;
        }
        return id;
    }

    private int eventos(String name) {
        int id = 0;
        String patron_alt1 = ".*[Oo]bra.*";
        String patron_alt2 = ".*[Tt]eatro.*";
        String patron_alt3 = ".*[Cc]ine.*";
        String patron_alt4 = ".*[Ee]vento.*";
        String patron_alt5 = ".*[Ss]ervicio.*";
        String patron_alt6 = ".*[Ff]iesta.*";
        String patron_alt7 = ".*[Ff]otograf.*";
        String patron_alt8 = ".*[Cc]urso.*";
        String patron_alt9 = ".*[Bb]aile.*";
        String patron_alt10 = ".*[Cc]olch.*";
        String patron_alt11 = ".*[Aa]sesor.*";
        String patron_alt12 = ".*[Cc]ertificac.*";
        String patron_alt13 = ".*[Cc]omedy.*";
        String patron_alt14 = ".*[Rr]ecuerd.*";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
        Pattern p_alt7 = Pattern.compile(patron_alt7);
        Pattern p_alt8 = Pattern.compile(patron_alt8);
        Pattern p_alt9 = Pattern.compile(patron_alt9);
        Pattern p_alt10 = Pattern.compile(patron_alt10);
        Pattern p_alt11 = Pattern.compile(patron_alt11);
        Pattern p_alt12 = Pattern.compile(patron_alt12);
        Pattern p_alt13 = Pattern.compile(patron_alt13);
        Pattern p_alt14 = Pattern.compile(patron_alt14);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
        Matcher matcher_texto7 = p_alt7.matcher(name);
        Matcher matcher_texto8 = p_alt8.matcher(name);
        Matcher matcher_texto9 = p_alt9.matcher(name);
        Matcher matcher_texto10 = p_alt10.matcher(name);
        Matcher matcher_texto11 = p_alt11.matcher(name);
        Matcher matcher_texto12 = p_alt12.matcher(name);
        Matcher matcher_texto13 = p_alt13.matcher(name);
        Matcher matcher_texto14 = p_alt14.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find())
                || (matcher_texto7.find()) || (matcher_texto8.find()) || (matcher_texto9.find())
                || (matcher_texto10.find()) || (matcher_texto11.find()) || (matcher_texto12.find())
                || (matcher_texto13.find()) || (matcher_texto14.find())) {
            id = 5;
        } else {
            id = 0;
        }
        return id;
    }

    private int salud_belleza(String name) {
        int id = 0;
        String patron_alt1 = ".*[Mm]edicina.*";
        String patron_alt2 = ".*[Cc]apilar.*";
        String patron_alt3 = ".*[Kk]eratina.*";
        String patron_alt4 = ".*[Oo]rtodoncia.*";
        String patron_alt5 = ".*[Dd]espistaje.*";
        String patron_alt6 = ".*[Pp]eluquer.*";
        String patron_alt7 = ".*[Cc]uerpo.*";
        String patron_alt8 = ".*[Cc]irug.*";
        String patron_alt9 = ".*[Dd]espistaje.*";
        String patron_alt10 = ".*[Ff]acial.*";
        String patron_alt11 = ".*[Ee]xfoliaci.*";
        String patron_alt12 = ".*[Ll]ipo.*";
        String patron_alt13 = ".*[Mm]ascarilla.*";
        String patron_alt14 = ".*[Dd]ental.*";
        String patron_alt15 = ".*[Mm]asaje.*";
        String patron_alt16 = ".*[Dd]epilac.*";
        String patron_alt17 = ".*[Cc]abello.*";
        String patron_alt18 = ".*[Pp]erfil 20.*";
        String patron_alt19 = ".*[Mm]icrodermo.*";
        String patron_alt20 = ".*[Cc]uello.*";
        String patron_alt21 = ".*[Tt]atuaje.*";
        String patron_alt22 = ".*[Aa]delgaza.*";
        String patron_alt23 = ".*[Cc]elulitis.*";
        String patron_alt24 = ".*[Mm]echa.*";
        String patron_alt25 = ".*[Dd]esintoxica.*";
        String patron_alt26 = ".*[Mm]anicur.*";
        String patron_alt27 = ".*[Vv]acumterapia.*";
        String patron_alt28 = ".*[Dd]renaje.*";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);
        Pattern p_alt4 = Pattern.compile(patron_alt4);
        Pattern p_alt5 = Pattern.compile(patron_alt5);
        Pattern p_alt6 = Pattern.compile(patron_alt6);
        Pattern p_alt7 = Pattern.compile(patron_alt7);
        Pattern p_alt8 = Pattern.compile(patron_alt8);
        Pattern p_alt9 = Pattern.compile(patron_alt9);
        Pattern p_alt10 = Pattern.compile(patron_alt10);
        Pattern p_alt11 = Pattern.compile(patron_alt11);
        Pattern p_alt12 = Pattern.compile(patron_alt12);
        Pattern p_alt13 = Pattern.compile(patron_alt13);
        Pattern p_alt14 = Pattern.compile(patron_alt14);
        Pattern p_alt15 = Pattern.compile(patron_alt15);
        Pattern p_alt16 = Pattern.compile(patron_alt16);
        Pattern p_alt17 = Pattern.compile(patron_alt17);
        Pattern p_alt18 = Pattern.compile(patron_alt18);
        Pattern p_alt19 = Pattern.compile(patron_alt19);
        Pattern p_alt20 = Pattern.compile(patron_alt20);
        Pattern p_alt21 = Pattern.compile(patron_alt21);
        Pattern p_alt22 = Pattern.compile(patron_alt22);
        Pattern p_alt23 = Pattern.compile(patron_alt23);
        Pattern p_alt24 = Pattern.compile(patron_alt24);
        Pattern p_alt25 = Pattern.compile(patron_alt25);
        Pattern p_alt26 = Pattern.compile(patron_alt26);
        Pattern p_alt27 = Pattern.compile(patron_alt27);
        Pattern p_alt28 = Pattern.compile(patron_alt28);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);
        Matcher matcher_texto4 = p_alt4.matcher(name);
        Matcher matcher_texto5 = p_alt5.matcher(name);
        Matcher matcher_texto6 = p_alt6.matcher(name);
        Matcher matcher_texto7 = p_alt7.matcher(name);
        Matcher matcher_texto8 = p_alt8.matcher(name);
        Matcher matcher_texto9 = p_alt9.matcher(name);
        Matcher matcher_texto10 = p_alt10.matcher(name);
        Matcher matcher_texto11 = p_alt11.matcher(name);
        Matcher matcher_texto12 = p_alt12.matcher(name);
        Matcher matcher_texto13 = p_alt13.matcher(name);
        Matcher matcher_texto14 = p_alt14.matcher(name);
        Matcher matcher_texto15 = p_alt15.matcher(name);
        Matcher matcher_texto16 = p_alt16.matcher(name);
        Matcher matcher_texto17 = p_alt17.matcher(name);
        Matcher matcher_texto18 = p_alt18.matcher(name);
        Matcher matcher_texto19 = p_alt19.matcher(name);
        Matcher matcher_texto20 = p_alt20.matcher(name);
        Matcher matcher_texto21 = p_alt21.matcher(name);
        Matcher matcher_texto22 = p_alt22.matcher(name);
        Matcher matcher_texto23 = p_alt23.matcher(name);
        Matcher matcher_texto24 = p_alt24.matcher(name);
        Matcher matcher_texto25 = p_alt25.matcher(name);
        Matcher matcher_texto26 = p_alt26.matcher(name);
        Matcher matcher_texto27 = p_alt27.matcher(name);
        Matcher matcher_texto28 = p_alt28.matcher(name);

        if ((matcher_texto1.find()) || (matcher_texto2.find()) || (matcher_texto3.find())
                || (matcher_texto4.find()) || (matcher_texto5.find()) || (matcher_texto6.find())
                || (matcher_texto7.find()) || (matcher_texto8.find()) || (matcher_texto9.find())
                || (matcher_texto10.find()) || (matcher_texto11.find()) || (matcher_texto12.find())
                || (matcher_texto13.find()) || (matcher_texto14.find()) || (matcher_texto15.find())
                || (matcher_texto16.find()) || (matcher_texto17.find()) || (matcher_texto18.find())
                || (matcher_texto19.find()) || (matcher_texto20.find()) || (matcher_texto21.find())
                || (matcher_texto22.find()) || (matcher_texto23.find()) || (matcher_texto24.find())
                || (matcher_texto25.find()) || (matcher_texto26.find()) || (matcher_texto27.find())
                || (matcher_texto28.find())) {
            id = 6;
        } else {
            id = 0;
        }
        return id;
    }

    private Deal Search_Price(Deal deal) {
        int id = 0;
        String name = deal.getName();

        String patron_alt1 = ".[Bs.][bs.].\\d+."; //Valida si contiene un digito
        String patron_alt2 = ".[Bs.][bs.].\\d+\\.\\d+."; //Valida si contiene un digito
        String patron_alt3 = "(.[Bs.][bs.].\\d+)";
        //String patron_alt3 = "(\\d+)";

        Pattern p_alt1 = Pattern.compile(patron_alt1);
        Pattern p_alt2 = Pattern.compile(patron_alt2);
        Pattern p_alt3 = Pattern.compile(patron_alt3);

        Matcher matcher_texto1 = p_alt1.matcher(name);
        Matcher matcher_texto2 = p_alt2.matcher(name);
        Matcher matcher_texto3 = p_alt3.matcher(name);

        if ((matcher_texto1.find())) {
            // System.out.println(matcher_texto1.group(0));
        }
        if ((matcher_texto2.find())) {

            //System.out.println(matcher_texto2.group(0));
        }
        int cat = 1;
        int price = 0;

        while ((matcher_texto3.find())) {

            String price_s = matcher_texto3.group(0);

            String patron_alt4 = "(\\d+)";
            Pattern p_alt4 = Pattern.compile(patron_alt4);

            Matcher matcher_texto4 = p_alt4.matcher(price_s);

            //System.out.println(matcher_texto4.group(0));
            if ((matcher_texto4.find())) {
                price = Integer.parseInt(matcher_texto4.group(0));
                //System.out.println(price);
            }

            if (cat == 1) {
                deal.setPrice(price);
            }
            if (cat == 2) {
                deal.setAmount(price);
            }

            cat++;
        }

        return deal;
    }
}
